< previous page page_223 next page >

Page 223
Solution 22
What Is That Mapped Drive? Part 2
When you step through the code, you'll find that the crash occurs at this line:
Call RtlMoveMemory(nr, OutputBuffer(0), Len(nr))
What could cause the crash? The nr parameter is correctit's a NETRESOURCE structure passed by reference. OutputBuffer(0) is the first byte in the byte array. Passed by reference, it will pass a pointer to the first byte of the array, which is also correct. The number of bytes being passed is the length of the NETRESOURCE structure. You know for a fact that the OutputBuffer array is at least as large as the NETRESOURCE structure because it is defined as containing the structure, so the WNetGetResourceInformation function should return a required size that is at least that size. If you check the OutputBufferSize variable, you will see that it is definitely larger than a NETRESOURCE structure.
That means the problem must be inside the structure.
When you copy the NETRESOURCE structure using RtlMoveMemory, the nr variable will be loaded with the exact data in the buffer. The fields that are defined as Longs won't be a problem. But what about the string variables? When defined as strings (as done in this example), Visual Basic expects each of those fields to contain a BSTR pointer to a Unicode string. However, the NETRESOURCE structure simply defines them as string pointers, and since we are using the ANSI version of the WNetGetResourceInformation function, those will be pointers to NULL-terminated ANSI strings.
It's not the ANSI-to-Unicode conversion that's killing us here, it's the fact that those pointers are definitely not BSTR pointers. Visual Basic will look at those values and more than likely crash right away. It will expect the length of the string to appear before the data. What values will it read in this case? Who knows? But you can be sure it will not be the actual length of the string. (See Tutorial 7, "Classes, Structures, and User-Defined Types," in Part III of this book, for more information on this problem.)
The solution is to change those parameters to the Long data type as follows:
Private Type NETRESOURCE
        dwScope As Long

 
< previous page page_223 next page >